package com.training1.core.modules.service;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.training1.core.common.exception.ExcelException;
import com.training1.core.common.service.CrudService;
import com.training1.core.common.utils.DateUtils;
import com.training1.core.common.utils.excel.ExcelUtil;
import com.training1.core.entity.BatchWithDrawalEntity;
import com.training1.core.entity.IncomeEntity;
import com.training1.core.entity.MemberEntity;
import com.training1.core.modules.dao.BatchWithDrawalDao;
import com.training1.core.modules.dao.MemberDao;
import com.training1.util.ImportExcel;

@Service
@Transactional
public class BatchWithDrawalService extends CrudService<BatchWithDrawalDao, BatchWithDrawalEntity> {
	@Resource
	private MemberDao mDao;
	/**
	 * 上传excel验证
	 * @param in
	 * @param name
	 * @return
	 * @throws ExcelException
	 */
	public Map<String, String> validate(InputStream in,String name) throws ExcelException {
			return ImportExcel.validate3(in, mDao, name);
		
	}

	public int addBatch(List<BatchWithDrawalEntity> list) {
		// TODO Auto-generated method stub
		return dao.addBatch(list);
	}
	/**
	 * 批量插入提现表数据并更新会员表中可提现数据
	 * @param list
	 * @return
	 */
	public int insertBatch(List<BatchWithDrawalEntity> batchList,List<MemberEntity> memList1) {
		dao.insertBatch(batchList);
		return mDao.updateBatch(memList1);
	}
	/**
	 * 单条插入提现表数据并更新会员表中可提现数据
	 * @param bde
	 * @param mem
	 * @return
	 */
	@Transactional
	public int saveOne(BatchWithDrawalEntity bde,MemberEntity mem) {
		dao.saveOne(bde);
		return mDao.updateCash(mem);
		
	}
	/**
	 * 通过审核和打款更新状态
	 * @param map
	 * @return
	 */
	public int updateStatus(Map<String,Object> map) {
		return dao.updateStatus(map);
		
	}
	/**
	 * 审核拒绝后更新提现表状态并更新会员表可提现数据
	 * @param map
	 * @param memList1
	 * @return
	 */
	public int updateStatus1(Map<String, Object> map, List<MemberEntity> memList1) {
		dao.updateStatus(map);
		return mDao.updateBatch(memList1);
		
	}
	/**
	 * 通过订单编号查出提现会员提现表中信息
	 * @param map
	 * @return
	 */
	public List<BatchWithDrawalEntity> findByOrderNumber(Map<String, Object> map) {
		return dao.findByOrderNumber(map);
	}
	/**
	 * 查询申请金额总和
	 * @param map
	 * @return
	 */
	public BatchWithDrawalEntity findAllMoney(Map<String,Object> map) {
		return dao.findAllMoney(map);
	}
	/**
	 * 更据手机号查会员
	 * @param cellPhone
	 * @return
	 */
	public MemberEntity findMemberByCell(String cellPhone) {
		return dao.findMemberByCell(cellPhone);
		
	}
	/**
	 * 更据手机号和开始记录数查找可提现记录
	 * @param map
	 * @return
	 */
	public List<BatchWithDrawalEntity> findApiList(Map<String,Object> map) {
		return dao.findApiList(map);
	}

	public void export(List<BatchWithDrawalEntity> list, HttpServletResponse resp) {
		//设置列标题
		List<String> excelHead = new ArrayList<String>();
		excelHead.add("订单编号");
		excelHead.add("会员手机号");
		excelHead.add("申请时间");
		excelHead.add("银行");
		excelHead.add("卡号");
		excelHead.add("持卡人姓名");
		excelHead.add("预留手机号");
		excelHead.add("申请金额");	
				
		//设置内容		
		List<List<String>> dataList = new ArrayList<List<String>>();
		for (BatchWithDrawalEntity bde : list) {
			List<String> data = new ArrayList<String>();
			data.add(bde.getOrderNumber());
			data.add(bde.getPhone());
			
			Date date = bde.getApplyTime();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dateString = formatter.format(date);
			data.add(dateString);
			
			data.add(bde.getBankforCard());
			data.add(bde.getCardNumber());
			data.add(bde.getName());
			data.add(bde.getCellphoneForCard());
			data.add(bde.getMoney().toString());
		
			dataList.add(data);
		}		
				
		//设置文件名
		String fileName;
		String sheetName = "sheet1";
		String titleName;
		fileName = "提现记录-待审核_"+new SimpleDateFormat("yyyy-MM-dd").format(new Date());
		titleName = "提现记录—待审核列表";
		
		ExcelUtil.exportExcel(resp, excelHead, dataList, fileName, sheetName,titleName);		
	}
}

